// index.js
// 获取应用实例
const app = getApp()
var requests = require('../../utils/request.js');
var utils = require('../../utils/util.js');
Page({
  data: {},
  async onLoad() {

    let commnetList = await this.getCommnetList();
    this.setData({
      commnetList,
    })

  },

  //左右滑动的方法 2022.11.1 Linyufan.com
  async swipeFun(e) {
    var _this = this
    console.log('删除', e)
    const {
      position,
      instance
    } = e.detail;

    let comment_id = e.currentTarget.dataset.comment_id;

    switch (position) {
      case 'left':
        const wxShowModalStatus = await utils.linYuFanShowModal('删除提醒', '确定删除此评论吗？');
        if (wxShowModalStatus) {
          wx.showLoading({
            title: '删除中...',
          })

          let verifyRes = await _this.verifyCommentList(comment_id, 'del');
          if (verifyRes == 'operate_succ') {
            wx.hideLoading()
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            })
            //审核成功了，重新载入当前页面
            let commnetList = await _this.getCommnetList();
            this.setData({
              commnetList,
            })
            instance.close();
          }

        } else {
          instance.close();
        }
        case 'cell':
          instance.close();
          break;
        case 'right':
          let verifyRes = await _this.verifyCommentList(comment_id, 'pass');
          if (verifyRes == 'operate_succ') {
            wx.showToast({
              title: '审核成功',
              icon: 'success'
            })
            //审核成功了，重新载入当前页面
            let commnetList = await _this.getCommnetList();
            this.setData({
              commnetList,
            })
            instance.close();
          } else {
            wx.showToast({
              title: '此评论不存在或已审核过了',
              icon: 'none'
            })
          }
          break;
    }


  },

  //审核评论 2022.11.1 Linyufan.com
  async verifyCommentList(comment_id, type) {
    console.log('需要审核的数据', comment_id, type)
    return new Promise((resolve) => {
      let request_data = {
        commentid: comment_id,
        type, //通过或者删除 del verify
      }
      requests.postAPI_verifyCommentData(request_data, (returndata) => {
        console.log('审核结果', returndata)
        resolve(returndata);
      })
    })
  },

  // 跳转至评论详情页面（传递参数 comment_id ） 2022.11.1 Linyufan.com
  goToCommentDetail(e) {
    console.log(e)
    let comment_id = e.currentTarget.dataset.comment_id;
    wx.navigateTo({
      url: '../commentDetail/commentDetail?comment_id=' + comment_id,
    })
  },

  //跳转到待审核列表 2022.11.1 Linyufan.com
  goToAwaitPass() {
    wx.navigateTo({
      url: '../commentAwaitPass/commentAwaitPass',
    })
  },


  //获取首页评论列表的方法 2022.11.1 Linyufan.com
  async getCommnetList() {
    return new Promise((resolve) => {
      let request_data = {}
      requests.postAPI_commentListData(request_data, (returndata) => {
        console.log('评论列表', returndata)
        resolve(returndata);

        //如果接口有返回code判断，最好判断下
        // if (returndata.code == 200 && returndata.msg == '操作成功') {
        //   resolve(returndata);
        // }
      })
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    let commnetList = await this.getCommnetList();
    this.setData({
      commnetList,
    })
    wx.showToast({
      title: '刷新成功',
    });
    wx.stopPullDownRefresh();
  },

})